#include "stdafx.h"
#include "ui_label.h"


_SSL_BEGIN
_SSUIL_BEGIN

UILabel::UILabel()
{
	//
	background_transparent_ = true;
	show_border_ = false;
	//
	// text_;
	text_color_ = 0xFFFFFF;
	// text_font_id_;
	text_style_ori_ = 0;
	text_style_ = 0;
	transparent_ = true;
}

UILabel::~UILabel()
{

}

bool UILabel::ApplyAttributes(IUIBuilderInterface *ui_builder, 
	IUIXMLDocumentInterface *xml_document, UIXmlNode node)
{
	if(!__super::ApplyAttributes(ui_builder, xml_document, node))
		return false;

	UIXmlAttribute attribute = NULL;

	attribute = xml_document->FirstAttribute(node, "text");
	if(attribute != NULL)
	{
		SetText(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
	}

	attribute = xml_document->FirstAttribute(node, "text_color");
	if(attribute != NULL)
	{
		unsigned long c = 0;
		if(UIValueParseUtil::ParseColor(xml_document->AttributeValue(attribute), c) == 1)
			SetTextColor(c);
	}

	attribute = xml_document->FirstAttribute(node, "text_font");
	if(attribute != NULL)
	{
		SetTextFontId(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
	}

	attribute = xml_document->FirstAttribute(node, "text_style");
	if(attribute != NULL)
	{
		unsigned int style = 0;
		const char *val = xml_document->AttributeValue(attribute);
		if(strstr(val, "left") != 0)
			style |= kTextStyleLeft;
		if(strstr(val, "center") != 0)
			style |= kTextStyleCenter;
		if(strstr(val, "right") != 0)
			style |= kTextStyleRight;
		if(strstr(val, "top") != 0)
			style |= kTextStyleTop;
		if(strstr(val, "venter") != 0)
			style |= kTextStyleVCenter;
		if(strstr(val, "bottom") != 0)
			style |= kTextStyleBottom;
		if(strstr(val, "single_line") != 0)
			style |= kTextStyleSingleLine;
		SetTextStyle(style);
	}

	attribute = xml_document->FirstAttribute(node, "text_background_transparent");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			SetTextBackgroundTransparent(b);
		}
	}

	return true;
}

void UILabel::SetPresetPosition(const UIPresetPosition &preset_pos)
{
	__super::SetPresetPosition(preset_pos);
	text_style_ = text_style_ori_;
	if(preset_pos.IsAutoWidth())
	{
		text_style_ |= kTextStyleSingleLine;
	}
}

int UILabel::GetControlAutoWidth()
{
	SS_ASSERT(SS_FLAG_ISSET(text_style_, kTextStyleSingleLine));
	scoped_ref_ptr<IUIManagerInterface> win_man = GetWindowManager();
	if(win_man == NULL)
		return 0;
	scoped_ref_ptr<IUIResourceRenderInterface> rr = win_man->GetResourceRener();
	if(rr == NULL)
		return 0;

	UIRect rt;
	if(!rr->MeasureTextSize(text_, text_.length(), rt, text_font_id_,
		kMeasureWidthAndHeight))
	{
		return 0;
	}

	return rt.Width();
}

int UILabel::GetControlAutoHeight()
{
	scoped_ref_ptr<IUIManagerInterface> win_man = GetWindowManager();
	if(win_man == NULL)
		return 0;
	scoped_ref_ptr<IUIResourceRenderInterface> rr = win_man->GetResourceRener();
	if(rr == NULL)
		return 0;

	if(!(preset_pos_.HasWidth() && !preset_pos_.IsAutoWidth() &&
		preset_pos_.Width() > 0))
	{
		text_style_ |= kTextStyleSingleLine;
	}

	if(SS_FLAG_ISSET(text_style_, kTextStyleSingleLine))
	{
		UIRect rt;
		if(!rr->MeasureTextSize(text_, text_.length(), rt, text_font_id_, kMeasureWidthAndHeight))
			return 0;
		return rt.Height();
	}else
	{
		SS_ASSERT(preset_pos_.HasWidth() && !preset_pos_.IsAutoWidth() &&
			preset_pos_.Width() > 0);
		UIRect rt;
		rt.right = rt.left + preset_pos_.Width();
		if(!rr->MeasureTextSize(text_, text_.length(), rt, text_font_id_, kMeasureHeight))
			return 0;
		return rt.Height();
	}

	return 0;
}

void UILabel::OnRenderForeground(const UIRenderContext &rc)
{
	rc.rr->DrawText(rc.dc, text_,
		text_.length(), text_color_, rc.client_pos_on_client,
		text_font_id_, text_style_, transparent_, GetBackgroundColor());
}

void UILabel::SetText(const tstring &text)
{
	text_ = text;
	SmartSetNeedUpdate(true);
}

void UILabel::SetTextColor(unsigned long color)
{
	text_color_ = color;
	Invalidate();
}

void UILabel::SetTextFontId(const tstring &font_id)
{
	text_font_id_ = font_id;
	SmartSetNeedUpdate(true);
}

void UILabel::SetTextStyle(unsigned int style)
{
	text_style_ori_ = style;
	SetPresetPosition(GetPresetPosition());
	Invalidate();
}

tstring UILabel::GetText() const
{
	return text_;
}

unsigned long UILabel::GetTextStyle()
{
	return text_style_;
}

tstring UILabel::GetTextFontId() const
{
	return text_font_id_;
}

void UILabel::SetTextBackgroundTransparent(bool transparent)
{
	transparent_ = transparent;
	Invalidate();
}


_SSUIL_END
_SSL_END
